<?php
class Administrator_ConfigController extends Zend_Controller_Action
{
	public function init() {
	}
	
	public function indexAction(){
	    $order_field             =  $this->_request->getParam('order',false);
        $type                    =  $this->_request->getParam('type',false);
		   
		$do = @$this->_request->getParam('do');
		$id = (int)$this->_request->getParam('id');
		if($do == 'delete' && $id >0){
			$this->deleteConfig($id);
		}elseif($do == 'list'){
			$this->getListConfig();
		}else{
			$keyword = $this->_request->getParam('keyword');
			$this->view->keyword 	= $keyword;
		}
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/config/index.js');
	}
	
	public function updateAction(){
		$objConfig = new HT_Model_administrator_models_config();
		$do 		 = @$this->_request->getParam('do');
		$id = (int)$this->_request->getParam('id');
		$status 	= (int)$this->_request->getParam('status');
		$groupId	= null;
		//echo $do; die();
		if($do == 'submit'){
			$active = (int)$this->_request->getParam('active');
			if($active != 1) $active = 0;
			$data = array();
			$data['category_name'] 		= $this->_request->getParam('category_name');
			$data['group_id'] 			= $this->_request->getParam('group_id');
			$data['active'] 			= $active;
			$data['stt'] 				= (int)$this->_request->getParam('stt');
			$data['description'] 		= $this->_request->getParam('description');
			if($id >0){
				$objConfig->update($data, 'id='.(int)$id);
			}else{
				$id = $objConfig->addData($data);
			}
			//$this->_redirect(WEB_PATH.'/administrator/config/update?status=1&id='.$id);
			$this->_redirect(WEB_PATH.'/administrator/config');
		}elseif($id >0){
			$config			= $objConfig->getConfig($id);
			$groupId				= $config['group_id'];
			$this->view->config = $config;
		}
		
		$objUtil 			= new HT_Model_administrator_models_utility();
		$newsGroup 			= $objUtil->GetCombobox('group_id','group_id','config_key','news_group',array('isBlankVal'=>'Vui lòng chọn nhóm tin','defaultValue'=>$groupId));
		
		$this->view->newsGroup  		= $newsGroup;
		
		$this->view->id = $id;
		$this->view->status 	 = $status;
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/config/update.js');
	}

	function deleteConfig($id){
		$objConfig = new HT_Model_administrator_models_config();
		echo $objConfig->delete("config_id=".(int)$id);die();
	}

	function getListConfig(){
	    
        $objUtil 		= new HT_Model_administrator_models_utility();
		$objConfig 		= new HT_Model_administrator_models_config();
		$keyword 		= trim($this->_request->getParam('keyword'));
		$page 			= (int)$this->_request->getParam('page');
		$size 			= PAGING_SIZE;
		if (!is_numeric($page) || $page <= 0) {
			$page = 1;
		}
		$start = $page * $size - $size;
		$totalRecord = $objConfig->getListConfig_nb(array('keyword'=>$keyword));
		$listConfig = $objConfig->getListConfig($start,$size,array('keyword'=>$keyword));
		$paging = trim($objUtil->paging($page, $size, $totalRecord));

		$ajaxData = null;
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		$ajaxData .= '<table cellspacing="0" class="tablesorter tablesorterBorder">';
		$ajaxData .= '<thead>';
			$ajaxData .= '<tr>';
				$ajaxData .= '<th width="15">STT</th>';
				$ajaxData .= '<th width="200">Key</th>';
				$ajaxData .= '<th width="200">Value</th>';
				$ajaxData .= '<th width="250">Description</th>';
				$ajaxData .= '<th style="white-space: nowrap;padding-right: 5px;" align="center">Điều khiển</th>';
			$ajaxData .= '</tr>';
		$ajaxData .= '</thead>';
		
		$i=0;
		$arrGroup = array();
		foreach($listConfig as $cfg){
			$i++;
			$trClass = null;
			if($i%2 == 1) $trClass = ' class="altrow"';
			$ajaxData .= '<tr id="'.$cfg['config_id'].'" '.$trClass.'>';
			$ajaxData .= '<td align="center">'.$i.'</td>';
			$ajaxData .= '<td>'.$cfg['config_key'].'</td>';
			$ajaxData .= '<td>'.$cfg['config_value'].'</td>';
			$ajaxData .= '<td>'.$cfg['description'].'</td>';
			$ajaxData .= '<td style="white-space: nowrap" align="center">';
			$ajaxData .= '<a href="#" onclick="deleteConfig('.$cfg['config_id'].')">Xóa</a> | <a href="'.WEB_PATH.'/administrator/config/update/?id='.$cfg['config_id'].'">Sửa</a>';
			$ajaxData .= '</td>';
			$ajaxData .= '</tr>';
		}
		$ajaxData .= '</tbody>';
		$ajaxData .= '</table>';
		$ajaxData .= '</div>';
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		echo $ajaxData; die();
	}
}
